package com.jhws.sys.cms.service;

import com.jhws.sys.cms.bo.CmsConstants;
import com.jhws.sys.cms.bo.CommGroup;
import com.jhws.common.base.bo.Global;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.sys.user.bo.UserCommsBo;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.sys.user.service.UserCommsService;
import org.apache.shiro.SecurityUtils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

@SuppressWarnings("unchecked")
@Service
public class CmsRedisService {
    @Resource
    private AppResourceService appResourceService;
    @Resource
    private RedisTemplate serializeTemplate;
    @Resource
    private UserCommsService userCommsService;
    @Resource
    private ITblUserDimService userDimService;

    //保存CMS到redis
    public void addRedisCMS(String typeCode, String commCode, List<Map<String, Object>> ret) {
        String logName;
        List<UserCommsBo> userCommsBoList = userCommsService.findUserCommsByWhere(" where comm_code = '" + commCode + "' and role_name='" + Global.ROLE_SEC_LEVEL_ADMIN + "'");
        if (userCommsBoList != null && userCommsBoList.size() > 0) {
            logName = userCommsBoList.get(0).getUserName();
        } else {
            logName = Global.ROLE_SUPPER_ADMIN;
        }
        HashOperations<String, String, List<Map<String, Object>>> valueOperations = serializeTemplate.opsForHash();
        valueOperations.put(CmsConstants.REDIS_CMS_HEAD + logName, CmsConstants.REDIS_CMS_HEAD + commCode + ":" + typeCode, ret);
    }

    //从Redis中获取CMS数据
    public List<Map<String, Object>> getRedisCMS(String typeCode, String commCode) throws IOException {
        String logName;
        List<UserCommsBo> userCommsBoList = userCommsService.findUserCommsByWhere(" where comm_code = '" + commCode + "' and role_name='" + Global.ROLE_SEC_LEVEL_ADMIN + "'");
        HashOperations<String, String, List<Map<String, Object>>> valueOperations = serializeTemplate.opsForHash();
        if (userCommsBoList != null && userCommsBoList.size() > 0) {
            logName = userCommsBoList.get(0).getUserName();
        } else {
            List<Map<String, Object>> ret = valueOperations.get(CmsConstants.REDIS_CMS_HEAD + Global.ROLE_SUPPER_ADMIN, CmsConstants.REDIS_CMS_HEAD + Global.DEFAULT_COMM + ":" + typeCode);
            if (ret == null) {
                ret = appResourceService.queryResourceListApi(typeCode, null, Global.DEFAULT_COMM);
            }
            addRedisCMS(typeCode, Global.DEFAULT_COMM, ret);
            return ret;
        }
        return valueOperations.get(CmsConstants.REDIS_CMS_HEAD + logName, CmsConstants.REDIS_CMS_HEAD + commCode + ":" + typeCode);
    }

    //清空CMS与帐号对应的缓存
    public void emptyRedisCMS() {
        //admin单独处理，因为它操作的是默认小区需要删除所有缓存
        if (SecurityUtils.getSubject().hasRole(Global.ROLE_SUPPER_ADMIN)) {
            emptyRedisCMSAll();
        } else {
            TblUserDimBo userDimBo = userDimService.currentUser();
            serializeTemplate.delete(CmsConstants.REDIS_CMS_HEAD + userDimBo.getLogName());
        }
    }

    //所有CMS缓存
    public void emptyRedisCMSAll() {
        Set<String> keys = serializeTemplate.keys(CmsConstants.REDIS_CMS_HEAD + "*");
        serializeTemplate.delete(keys);
    }

    //添加小区与组的缓存
    public void addRedisCommGroup(String commCode, List<CommGroup> groupList) {
        HashOperations<String, String, List<CommGroup>> commGroupHash = serializeTemplate.opsForHash();
        commGroupHash.put(CmsConstants.REDIS_COMM_GROUP_HEAD, CmsConstants.REDIS_COMM_GROUP_HEAD + commCode, groupList);
    }

    //获取小区与组的缓存
    public List<CommGroup> getRedisCommGroup(String commCode) {
        HashOperations<String, String, List<CommGroup>> commGroupHash = serializeTemplate.opsForHash();
        return commGroupHash.get(CmsConstants.REDIS_COMM_GROUP_HEAD, CmsConstants.REDIS_COMM_GROUP_HEAD + commCode);
    }

    //清空组与小区对应的缓存
    public void emptyRedisCommGroup() {
        TblUserDimBo userDimBo = userDimService.currentUser();
        List<UserCommsBo> userCommsBoList = userCommsService.findUserCommsByName(userDimBo.getLogName());
        HashOperations<String, String, String> commGroupHash = serializeTemplate.opsForHash();
        for (UserCommsBo userCommsBo : userCommsBoList) {
            commGroupHash.put(CmsConstants.REDIS_COMM_GROUP_HEAD, CmsConstants.REDIS_COMM_GROUP_HEAD + userCommsBo.getCommCode(), null);
        }
        //清空小区组证明小区组有改变，资源缓存也需要清空
        emptyRedisCMS();
    }

    //清空组与小区所有的缓存
    public void emptyRedisCommGroupAll() {
        serializeTemplate.delete(CmsConstants.REDIS_COMM_GROUP_HEAD);
    }
}
